"""
---------------------------------------------------------------------
- File: consumer_main.py
- Date: 2025/5/29
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 消费者主程序
---------------------------------------------------------------------
"""

import pika, sys, os

def simple_main():
    # 回调函数
    def callback(ch, method, properties, body):
        print(f" [x] Received: {body}")

    # 连接RabbitMQ
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host="localhost"))

    # 创建通道
    channel = connection.channel()

    # 声明交换机
    channel.queue_declare(queue="hello")

    # 消费消息
    channel.basic_consume(
        queue="hello",  # 队列名
        auto_ack=True,  # 自动确认
        on_message_callback=callback  # 回调函数
    )
    # 开始消费
    print("等待消息就绪...")
    channel.start_consuming()

if __name__ == '__main__':
    try:
        simple_main()
    except KeyboardInterrupt:
        print(" [x] Bye Bye!")
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)
